package com.loren.demo11.config;

import com.loren.demo11.interceptor.RetryClientHttpRequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Collections;

@Configuration
public class BeanConfig {

    @Resource
    private RetryClientHttpRequestInterceptor retryClientHttpRequestInterceptor;

    @Bean
    public RestTemplate getRestTemplate() {
        // 响应流可重复读
        RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
        restTemplate.setInterceptors(Collections.singletonList(this.retryClientHttpRequestInterceptor));
        return restTemplate;
    }
}
